package estore;



import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;

public class EStoreServer {
    private ProductService3 productService;
    private ServerSocket serverSocket;
    private boolean running;

    public EStoreServer(int port) {
        try {
            this.productService = new ProductService3();
            this.serverSocket = new ServerSocket(port);
            this.running = true;
            System.out.println("服务器已启动，监听端口: " + port);
        } catch (IOException e) {
            System.err.println("服务器启动失败: " + e.getMessage());
        }
    }

    public void start() {
        while (running) {
            try {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端连接: " + clientSocket.getInetAddress());

                // 为每个客户端创建新线程处理
                new Thread(() -> handleClient(clientSocket)).start();

            } catch (IOException e) {
                if (running) {
                    System.err.println("接受客户端连接失败: " + e.getMessage());
                }
            }
        }
    }

    private void handleClient(Socket clientSocket) {
        try (ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
             ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream())) {

            // 读取客户端请求
            String request = (String) ois.readObject();
            System.out.println("收到客户端请求: " + request);

            // 根据请求类型处理
            Object response = processRequest(request);

            // 发送响应
            oos.writeObject(response);
            oos.flush();
            System.out.println("已发送响应给客户端");

        } catch (IOException | ClassNotFoundException e) {
            System.err.println("处理客户端请求时出错: " + e.getMessage());
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                System.err.println("关闭客户端连接时出错: " + e.getMessage());
            }
        }
    }

    private Object processRequest(String request) {
        switch (request) {
            case "GET_ALL_PRODUCTS":
                Map<String, Product3> productsMap = (Map<String, Product3>) ProductService3.getAllProducts();
                return List.copyOf(productsMap.values());

            case "GET_PRODUCT":
                // 这里需要实现获取单个商品的逻辑
                return null;

            case "ADD_PRODUCT":
                // 这里需要实现添加商品的逻辑
                return null;

            default:
                return "未知请求: " + request;
        }
    }

    public void stop() {
        running = false;
        try {
            if (serverSocket != null && !serverSocket.isClosed()) {
                serverSocket.close();
            }
        } catch (IOException e) {
            System.err.println("关闭服务器时出错: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        EStoreServer server = new EStoreServer(8888);
        server.start();
    }
}